<template>
    <div class="top">
      <svg class="icon" aria-hidden="true" @click="goBack">
        <use xlink:href="#icon-xiangzuojiantou"></use>
      </svg>
      <span>登录金优陪</span>
    </div>
    <input v-model="email" type="text" placeholder="邮箱">
    <input v-model="password" class="inputpassword" type="password" placeholder="密码">
    <svg class="icon" aria-hidden="true" @click="togglePasswordVisibility">
      <use xlink:href="#icon-yanjing_xianshi"></use>
    </svg>
    <div class="agreement">
      <input type="checkbox" class="inputchenked" v-model="isChecked" id="checkbox">
      我已阅读并同意<span>《金优陪使用协议》和《隐私政策》</span>
    </div>
  
    <!-- 只有当复选框勾选了才能登录 -->
    <button @click="login" :disabled="!isChecked" :style="{ backgroundColor: isChecked ? 'rgba(0, 186,173, 100%)' : 'gray' }">登录</button>
    <div class="select">
      <span class="loginleft">忘记密码或用户名</span>
      <span class="loginright" @click="goToRegister">立即注册</span>
    </div>
    <div class="otherlogin">其他方式登录</div>
    <div class="selectlogin">
      <div class="loginitem">
        <img src="/image12.png" alt="">
        <span class="textapp">微信</span>
      </div>
      <div class="loginitem">
        <img src="/image13.png" alt="">
        <span class="textapp">腾讯QQ</span>
      </div>
      <div class="loginitem">
        <img src="/image14.png" alt="">
        <span class="textapp">新浪微博</span>
      </div>
      <div class="loginitem">
        <img src="/image15.png" alt="">
        <span class="textapp">iOS登录</span>
      </div>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  import { mapMutations } from 'vuex';
  
  export default {
    data() {
      return {
        email: '',
        password: '',
        isChecked: false,
        showPassword: false // 用于显示/隐藏密码
      };
    },
    methods: {
      ...mapMutations(['setUser']), // 从 Vuex store 引入 mutation
      goBack() {
        this.$router.push('myinfo');
      },
      async login() {
        try {
          const response = await axios.post('http://localhost:5000/api/auth/login', {
            email: this.email,
            password: this.password
          });
          alert(response.data.message); // 显示登录成功消息
          if (response.status === 200) {
            this.setUser(response.data.user); // 更新 Vuex store 中的用户信息
            this.$router.push('myinfo'); // 登录成功后跳转
            // console.log(response.data)
            // console.log(response.data.user.username)
          }
        } catch (error) {
          alert(error.response.data.message); // 显示错误消息
        }
      },
      togglePasswordVisibility() {
        this.showPassword = !this.showPassword;
      },
      goToRegister() {
        this.$router.push('register');
      }
    }
  };
  </script>
  
  <style scoped>
  /* 保持原样的样式 */
  .top {
    width: 7.86rem;
    height: 2.68rem;
    background-image: linear-gradient(to bottom, rgba(55, 170, 176, 87%), white);
    position: relative;
  }
  .top .icon {
    position: absolute;
    top: 50px;
    left: 10px;
  }
  .top span {
    position: absolute;
    top: 50px;
    left: 150px;
    font-size: 20px;
  }
  input {
    width: 7rem;
    height: 1.28rem;
    margin-left: 17px;
    border-radius: 5px;
    border: 1px solid gray;
  }
  .inputpassword {
    border-top: none;
    position: relative;
  }
  .icon {
    position: absolute;
    top: 4.5rem;
    right: 0.6rem;
  }
  .agreement {
    display: flex;
    margin-top: 20px;
    font-size: 12px;
    justify-content: center;
  }
  input[type="checkbox"] {
    width: 14px;
    height: 14px;
  }
  .agreement span {
    color: #00BAAD;
  }
  button {
    width: 7rem;
    height: 1.28rem;
    margin-left: 0.4rem;
    color: #FFFFFF;
    border: none;
    background-color: rgba(0, 186, 173, 100%);
    margin-top: 0.4rem;
  }
  .select {
    margin-top: 0.3rem;
    color: #00BAAD;
  }
  .loginleft {
    margin-left: 0.7rem;
  }
  .loginright {
    margin-right: 0.7rem;
    float: right;
  }
  .otherlogin {
    margin-top: 1rem;
    display: flex;
    color: #808080;
    font-size: 18px;
    justify-content: center;
    align-items: center;
  }
  .selectlogin {
    display: flex;
    justify-content: space-around;
    width: 100%;
  }
  .loginitem img {
    margin-top: 30px;
    width: 1.2rem;
    height: 1.2rem;
    display: block;
    margin-bottom: 20px;
  }
  .textapp {
    margin-left: 6px;
  }
  </style>
  